/*
	[Destoon B2B System] Copyright (c) 2008-2011 Destoon.COM
	This is NOT a freeware, use is subject to license.txt
*/
function check_mode(c, m) {
	var mode_num = 0; var e = Dd('com_mode').getElementsByTagName('input');	
	for(var i=0; i<e.length; i++) {if(e[i].checked) mode_num++;}
	if(mode_num > m) {confirm(lang(L['max_mode'], [m])); c.checked = false;}
}
function addop(id, v, t) {var op = document.createElement("option"); op.value = v; op.text = t; Dd(id).options.add(op);}
function delop(id) {
	var s = -1;
	for(var i = 0; i < Dd(id).options.length; i++) {if(Dd(id).options[i].selected) {s = i; break;}}
	if(s == -1) {alert(L['choose_category']); Dd(id).focus();} else {Dd(id).remove(s);}
}
function addcate(maxLength) {
	// 所选择的低级别的分类
	var selectedCatid = Dd('catid_1').value;
	// 内容选项的长度
	var nowLength = Dd('cates').options.length;
	if ( nowLength >= maxLength ) {
		alert(lang(L['max_cate'], [maxLength]));
		return;
	}
	for (var i = 0; i < nowLength; i++) {
		if ( Dd('cates').options[i].value == selectedCatid ) {
			alert(L['category_chosen']);
			return;
		}
	}
	var sourceSelectElements = Dd('cate').getElementsByTagName('select');
	var aimText = str = '';
	for (var i = 0; i < sourceSelectElements.length; i++) {
		if ( sourceSelectElements[i].value ) {
			str = sourceSelectElements[i].options[sourceSelectElements[i].selectedIndex].innerHTML;
			aimText += str + '/';
			str = '';
		}
	}
	if ( aimText ) {
		aimText = aimText.replace('&amp;', '&');
		// delete backslashes
		aimText = aimText.substring(0, aimText.length - 1);


		var afc = getFirstCate(aimText)
		for (var i = 0; i < nowLength; i++) {
			var nfc = getFirstCate($('#cates>option').eq(i).text());
			if (nfc != afc) {
				alert(lang(L['cate_first_notrepeat'], [nfc]));
				return ;
			}
		}
		addop('cates', selectedCatid, aimText);
		Dd('catid').value = Dd('catid').value ? Dd('catid').value + selectedCatid + ',' : ',' + selectedCatid + ',';
	} else {
		alert(L['choose_category']);
	}
}
function getFirstCate(str) {
	var checkMain = '';
	if (str.indexOf('/') < 0) {
		checkMain = str;
	} else {
		checkMain = str.substring(0, str.indexOf('/'));
	}
	return checkMain;
}
function delcate() {
	var s = -1;
	for(var i = 0; i < Dd('cates').options.length; i++) {if(Dd('cates').options[i].selected) {s = i; break;}}
	if(s == -1) {alert(L['choose_category']); Dd('cates').focus();} else {Dd('catid').value = Dd('catid').value.replace(','+Dd('cates').options[s].value+',', ','); Dd('cates').remove(s);}
}